/*
========================================
			iTools 用户统计系统
			     2013-12-27
========================================
{"op":"stat","type":"active", "param":{"product":"itoolsv1", "start":"2013-12-13", "end":"2013-12-19"}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"retention", "param":{"product":"itoolsv1", "start":"2013-12-13", "end":"2013-12-19", "p":1, "l":1, "v":1830}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"retention","param":{"product":"itoolsv1","start":"2013-11-30","end":"2013-12-30","granularity":"day","p":"1"}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"new","param":{"product":"itoolsv1","start":"2013-11-30","end":"2013-12-30","granularity":"day","p":"1"}}'
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","type":"retention","param":{"product":"itoolsv1","start":"2013-12-23","end":"2013-12-30","granularity":"day"}}'
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","viewType":"block","type":"retained","chartType":"line&bar","param":{"product":"itoolsv1","start":"2013-12-24","end":"2013-12-31","granularity":"day"}}
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","type":"new","param":{"product":"itoolsv1","start":"2013-12-23","end":"2013-12-30","granularity":"day","p":2}}'
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","type":"setup","param":{"product":"itoolsv2","start":"2013-12-12","end":"2014-01-08","granularity":"week"}}'
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","type":"setup_c","param":{"product":"itoolsv2","start":"2013-12-12","end":"2014-01-14"}}'
curl http://stat2.itools.hk/report -d '{"url":"http://stat2.itools.hk/report","op":"stat","type":"total","param":{"product":"itoolsv1","start":"2013-12-23","end":"2013-12-30","granularity":"day"}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"timedist","param":{"start":"2014-01-21","end":"2014-01-23"}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"launch","param":{"product":"itoolsv1","start":"2014-01-17","end":"2014-01-23","granularity":"day","p":6}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"dlapp","param":{"start":"2014-01-23","end":"2014-01-23","granularity":"hour"}}'
curl http://stat2.itools.hk/report -d '{"op":"stat","type":"crash","param":{"product":"itoolsv1","start":"2014-01-17","end":"2014-01-23","granularity":"day","p":6}}'

p值含义：
1 = iTools Win版
2 = iTools Mac版
7 = 兔子助手客户端 Mac
10 = iTools 金山版
12 = iTools 电信版
L:
"win":{
	"1":"简体中文",
	"2":"繁体中文(TW)",
	"3":"英语",
	"4":"日语",
	"5":"繁体中文(HK)",
	"6":"法语",
	"7":"俄语",
	"8":"葡萄牙语",
	"9":"越南语"
},
"mac":{
	"1":"英语",
	"2":"简体中文",
	"3":"繁体中文"
}


*/

/*tool: debug
---------------------------------------------------------------------------------------------------*/
var isDebug = false;
window.debug = (function() {
	var _window = "<dl id='itools_debug' style='text-align:left;width:500px;background:#fff; position:absolute;z-index:9999999999;left:0px;top:0px;border:1px solid #d00;color:#d00;'><dt style='background:#d00;color:#fff;line-height:1.5em'><b>Debug Window</b> <a style='color:#fff;text-decoration:underline' id='debug_clear' href='javascript:void(0)'>Clear</a> <a href='javascript:void(0)' id='debug_close' style='color:#fff;text-decoration:underline'>Close</a></dt><dd style='height:200px;overflow-y:scroll;padding:10px;'></dd></dl>";
	isDebug && $("body").append(_window);
	$("#debug_clear").bind("click", function() {
		$("#itools_debug dd").html("");
	});
	$("#debug_close").bind("click", function() {
		$("#itools_debug").hide();
	});
	if (isDebug) {
		var dragging = false;
		var iX, iY;
		$("#itools_debug dt").mousedown(function(e) {
			dragging = true;
			iX = e.clientX - $(this).parent().offset().left;
			iY = e.clientY - $(this).parent().offset().top;
			this.setCapture && this.setCapture();
			return false;
		});
		document.onmousemove = function(e) {
			if (dragging) {
				var e = e || window.event;
				var oX = e.clientX - iX;
				var oY = e.clientY - iY;
				$("#itools_debug").css({
					"left": oX + "px",
					"top": oY + "px"
				});
				return false;
			}
		};
		$(document).mouseup(function(e) {
			dragging = false;
			$("#itools_debug")[0].releaseCapture();
			e.cancelBubble = true;
		});
	}
	return {
		log: function(str) {
			if (!isDebug) {
				return false;
			}
			var str2 = "";
			if (typeof(str) == "object") {
				for (var i in str) {
					str2 += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + i + ":" + str[i] + "<br/>";
				}
				$("#itools_debug dd").append(typeof(str) + ":<br/>");
				$("#itools_debug dd").append(str2 + "<br/>");
			} else {
				$("#itools_debug dd").append(str + "<br/>");
			}
		}
	}
}());

var getLength = function(obj) {
	var _n = 0;
	for (var i in obj) {
		if (obj.hasOwnProperty(i)) {
			_n++;
		}

	}
	return _n;
}
require.config({
	paths: {
		model: './js/iTools/model',
		view: './js/iTools/view'
	}
});
require(['model', 'view'], function(m, v) {
	window.statisticsUIcontrol = statisticsUIcontrol(m, v);
});


/*根据给定天数，算起止时间*/
function getTimeArr(d) {
	var _stime = (new Date()).getTime();
	var day = 24 * 60 * 60 * 1000;
	var _etime = _stime - day * d;
	var getDateFormat = function(time) {
		var _time = new Date(time);
		var str = _time.toLocaleDateString();
		str = str.replace(/年|月/ig, "-").replace(/日/, "");
		return str;
	}
	return {
		start: getDateFormat(_stime),
		end: getDateFormat(_etime)
	};
}
/*loading UI
---------------------------------------------------------------------------------------------------*/
var loading = (function() {
	var _loading = $("#loading");
	return {
		show: function() {
			_loading.show();
		},
		hide: function() {
			_loading.hide();
		}
	}
}());
/*msg UI
---------------------------------------------------------------------------------------------------*/
var message = (function() {
	var _msg = $("#msg");
	return {
		show: function(msg) {
			_msg.html(msg).show();
		},
		hide: function() {
			_msg.hide();
		}
	}
}());
/*数组去重
---------------------------------------------------------------------------------------------------*/
function distinctArray2(arr) {
	var temp = arr.slice(0); //数组复制一份到temp
	for (var i = 0; i < temp.length; i++) {
		for (j = i + 1; j < temp.length; j++) {
			if (temp[j] == temp[i]) {
				temp.splice(j, 1); //删除该元素
				j--;
			}
		}
	}
	return temp;
}

/*base config
---------------------------------------------------------------------------------------------------*/
var defaultDate = getTimeArr(14);
var productTit = {
	"1": "iTools Win",
	"2": "iTools Mac",
	"6": "iTools v2.0 win",
	"7": "兔子助手客户端 Mac",
	"10": "iTools 金山",
	"12": "iTools 电信"
}

var languageTit = {
	"win": {
		"1": "简体中文",
		"2": "繁体中文(TW)",
		"3": "英语",
		"4": "日语",
		"5": "繁体中文(HK)",
		"6": "法语",
		"7": "俄语",
		"8": "葡萄牙语",
		"9": "越南语"
	},
	"mac": {
		"1": "英语",
		"2": "简体中文",
		"3": "繁体中文"
	}
}
var config = {
	"url": "http://stat2.itools.hk/report",
	"op": "stat",
	"viewType": "line&bar",
	"type": "new",
	"param": {
		"product": "itoolsv1",
		"start": defaultDate.end,
		"end": defaultDate.start,
		"granularity": "day"
	}
}
/*module: UIcontrol
---------------------------------------------------------------------------------------------------*/
	function statisticsUIcontrol(m, v) {
		var
		nav_ios = $("#nav_ios"),
		nav_itools = $("#nav_itools"),
		nav_user = $("#nav_user"),
                nav_ipa2exe = $("#nav_ipa2exe"),
                nav_others = $("#nav_others"),
		menuStartData = $("#s_time"),
		menuEndData = $("#e_time"),
		menuLanguage = $("#menu_language"),
		menuVersion = $("#menu_version"),
		menuChannel = $("#menu_channel"),
		menu_launch = $("#menu_launch"),
		menu_collapse=$("#menu_collapse"),
		menu_timedist=$("#menu_timedist"),
		menu_down_speed=$("#menu_down_speed"),
		menu_down_count=$("#menu_down_count"),
		menuDay = $("#menu_day"),
		menuWeek = $("#menu_week"),
		menuMonth = $("#menu_month"),
		menu30Day = $("#menu_30day"),
		menu7Day = $("#menu_7day"),
		menuHour = $("#menu_hour"),
		time_7 = getTimeArr(7),
		time_30 = getTimeArr(30),
		now = defaultDate.start,
		add_btn = $("#add_line_btn"),
		active_message = $("#active_message"),
		option_list = $("#option_list"),
		dropdownMenu = $(".dropdown-menu"),
		_options,
		option_list_add_n = 0,
		moreline = false,
		_data,
		_chartLock = false,
		chartsBox = $("#charts"),
		blockBox = $("#block"),
		timeSelectionReset=function(){
			$("#time_selection button").removeClass("active");
			if(config.param.granularity=="day" && config.param.start == time_30.end  && config.param.end == time_30.start){
				menu30Day.addClass("active");
				return true;
			}

			if(config.param.granularity=="day" && config.param.start == time_7.end && config.param.end == time_7.start){
				menu7Day.addClass("active");
				return true;
			}
			$("#time_selection button").removeClass("active");

			switch(config.param.granularity){
				case "hour":
				menuHour.addClass("active");
				break;

				case "day":
				menuDay.addClass("active");
				break;

				case "week":
				menuWeek.addClass("active");
				break;

				case "month" :
				menuMonth.addClass("active");
				break;
			}

			
		},
		configClear = function() {
			delete config.param.p;
			delete config.param.v;
			delete config.param.l;
			_options = {};
			menuVersion.find(".dropdown-menu").html("");
			menuChannel.find(".dropdown-menu").html("");
			menuLanguage.find(".dropdown-menu").html("");
			config.param.granularity = "day";
			config.param.start = defaultDate.end;
			config.param.end == defaultDate.start;
                        //每次加载默认值，测试用
                        config.url = "http://stat2.itools.hk/report";
			$(".select_text").html("全部");
		},
		init = function() {
			loading.show();
			timeSelectionReset();
			menuStartData.val(config.param.start);
			menuEndData.val(config.param.end);
			switch (config.viewType) {
				case "line":
					chartsBox.show();
					blockBox.hide();
					if (!moreline) {
						switch (config.type) {
							default: 
								_data = {};
								m.get({
									conf: config,
									callback: function(msg) {
										loading.hide();
										_options = msg.option;
										createMenu();
										v.createChart('charts', msg);
									}
								});
							break;

							case "timedist":
								m.getNative({
									conf: config,
									callback: function(msg) {
										loading.hide();
										_options = msg.options;
										createMenu();
										v.createChartForTimedist('charts', msg.stats);
									}
								});
							break;

							case "dlapp":
								m.getDlapp({
									conf:config,
									callback:function(msg){
										loading.hide();
										_options = msg.option;
										createMenu();
										v.createChartForDlapp('charts',msg);
									}
								});
							break;
						}
					} else {
						m.get({
							conf: config,
							callback: function(msg) {
								var _d;
								var filters;
								if (_data.xdata == undefined) {
									_data.xdata = {};
								}
								for (var i in msg.xdata) {
									_d = msg.xdata[i];
								}

								option_list.find(".filter_field").each(function(){
									var key = $(this).html();
									if(!_data.xdata.hasOwnProperty(key)){
										_data.xdata[key] = _d;
									}
									filters += key+"|";
						                        });

								for(var i in _data.xdata){
									if(!(new RegExp(i,"ig")).test(filters)){
										delete _data.xdata[i];
									}
								}
								_data.timeline = msg.timeline;
								loading.hide();
								_options = msg.option;
								createMenu();
								v.createChart('charts', _data);
							}
						});
					}
					break;

				case "block":
					chartsBox.hide();
					blockBox.show();
					m.getRetention({
						conf: config,
						callback: function(msg) {
							loading.hide();
							createMenu();
							v.createBlock('#block', msg);
						}
					});
					break;

				case "line&bar":
					chartsBox.show();
					blockBox.hide();
					_data = {};
					config.type = "active";
					m.get({
						conf: config,
						callback: function(msg) {
							_data = msg;
							config.type = "new";
							m.get({
								conf: config,
								callback: function(msg) {
									loading.hide();
									for (var i in msg.xdata) {
										_data.xdata[i] = msg.xdata[i];
									}
									_data.linetype = ["line", "bar"];
									_options = msg.option;
									createMenu();
									v.createChart('charts', _data);
								}
							});
						}
					});
					break;


				case "morebar":
					chartsBox.show();
					blockBox.hide();
					m.getDlapp({
						conf:config,
						callback:function(msg){
							loading.hide();
							_options = msg.option;
							createMenu();
							v.createChartForDlappcount('charts',msg);
						}
					});
					break;
			}
		},
		resetSubmenu = function() {
			$("#nav-tabs-group li").removeClass("active").eq(0).addClass("active");
		};

		init();
		add_btn.hide();
		option_list.hide();

		option_list.clear = function() {
			_data = {};
			$(this).find("tbody").html("");
		}
		option_list.add = function() {
			option_list_add_n++;
			var _length = option_list.find("tbody").find("tr").size() + 1;
			var _innerHTML = "<tr><td class='filter_field'>筛选条件" + option_list_add_n + "</td><td>" + menuChannel.find(".select_text").html() + "</td><td>" + menuLanguage.find(".select_text").html() + "</td><td>" + menuVersion.find(".select_text").html() + "</td><td><button class='btn btn-xs btn-danger' style='vertical-align:1px;' onclick='statisticsUIcontrol.delChart(this)'>删除</button></td></tr>";
			if (!_chartLock) {
				$(this).find("tbody").find("tr:last").remove();
			}
			$(this).find("tbody").append(_innerHTML);
		}

		//options Event 下拉选项事件
		var optionsEvent = function(e) {
			var _target = $(e.target);
			_target.parent().parent().parent().find(".select_text").html(_target.html());
			if (moreline) {
				_chartLock = false;
				option_list.add();
			}
			init();
		}



		//主菜单事件：itools v2.0
		nav_ios.click(function() {
			configClear();
			menuDay.show();
			menuWeek.show();
			menuHour.hide();
			menuMonth.show();
			$("#nav-tabs-group").show();
			$("#nav-tabs-group2").hide();
			active_message.show();
			$("#parameditor").show();
			resetSubmenu();
			config.viewType = "line&bar";
			$("#main_nav li").removeClass("active");
			$("#menu_setup,#menu_setup_c,#menu_collapse").show();
			$(this).parent().addClass("active");
			config.param.product = "itoolsv2"
			activation_module.reset("itoolsv2");
			$("h1").html("iTools v2.0");
			init();
		});

		//主菜单事件：itools
		nav_itools.click(function() {
			configClear();
			menuDay.show();
			menuWeek.show();
			menuHour.hide();
			menuMonth.show();
			$("#nav-tabs-group").show();
			$("#nav-tabs-group2").hide();
			active_message.show();
			$("#parameditor").show();
			resetSubmenu();
			config.viewType = "line&bar";
			$("#main_nav li").removeClass("active");
			$("#menu_setup,#menu_setup_c,menu_collapse").hide();
			$(this).parent().addClass("active");
			config.param.product = "itoolsv1"
			activation_module.reset("itoolsv1");
			$("h1").html("iTools");
			init();
		});

		//主菜单事件：用户参与度
		nav_user.click(function() {
			configClear();
			moreline = false;
			$("#main_nav li").removeClass("active");
			$("#nav-tabs-group").hide();
                        $("#nav-tabs-group3").hide();
                        $("#nav-tabs-group4").hide();
			$("#nav-tabs-group2").show();
			active_message.hide();
			menuWeek.hide();
			menuMonth.hide();
			menuHour.hide();
			$(this).parent().addClass("active");
			config.param.product = "itoolsv1"
			config.type = "timedist";
			config.viewType = "line";
			$("h1").html("用户参与度");
			init();
		});
                
                //主菜单事件：ipa2exe统计
		nav_ipa2exe.click(function() {
			configClear();
			moreline = false;
			$("#main_nav li").removeClass("active");
			$("#nav-tabs-group").hide();
                        $("#nav-tabs-group2").hide();
			$("#nav-tabs-group3").show();
                        $("#nav-tabs-group4").hide();
			active_message.hide();
			menuWeek.hide();
			menuMonth.hide();
			menuHour.hide();
			$(this).parent().addClass("active");
			config.param.product = "ipa2exe"
			config.type = "ipa2exe";
			config.viewType = "line";
                        config.url = "http://stat2.itools.hk/report2";
			$("h1").html("ipa2exe打包统计");
			init();
		});
                
                //其他统计
                nav_others.click(function() {
			configClear();
			moreline = false;
			$("#main_nav li").removeClass("active");
			$("#nav-tabs-group").hide();
                        $("#nav-tabs-group2").hide();
			$("#nav-tabs-group3").hide();
                        $("#nav-tabs-group4").show();
			active_message.hide();
			menuWeek.hide();
			menuMonth.hide();
			menuHour.hide();
			$(this).parent().addClass("active");
			config.param.product = "function"
			config.type = "function";
			config.viewType = "line";
                        config.url = "http://127.0.0.1/statistics_script/report.php";
                        config.url = "http://stat2.itools.hk/report2";
			$("h1").html("功能点统计");
			init();
		});



		//菜单选项事件
		$(".nav-tabs li").click(function(){
			configClear();
			$(".nav-tabs li").removeClass("active");
			$(this).addClass("active");
			var _id = $(this).attr("id");
			moreline = false;
			active_message.hide();
			add_btn.hide();
			option_list.hide();
			menuHour.hide();  
			switch(_id){
				//数据总览
				case "menu_all":
				config.viewType = "line&bar";
				active_message.show();
				break;

				//安装量汇总
				case "menu_setup":
				config.viewType = "line";
				config.type = "setup";
				break;

				//安装渠道汇总
				case "menu_setup_c":
				config.viewType = "line";
				config.type = "setup_c";
                                config.url = "http://stat2.itools.hk/report2";
				break;

				//总量
				case "menu_total":
				config.viewType = "line";
				config.type = "total";
				break;

				//新增用户统计
				case "menu_new":
				moreline = true;
				_data = {};
				config.viewType = "line";
				config.type = "new";
				add_btn.show();
				option_list.show();
				option_list.clear();
				option_list.add();
				break;

				//活跃用户统计
				case "menu_active":
				moreline = true;
				_data = {};
				config.viewType = "line";
				config.type = "active";
				add_btn.show();
				option_list.show();
				option_list.clear();
				option_list.add();
				break;

				//启动次数
				case "menu_launch":
				config.viewType = "line";
				config.type = "launch";
				break;

				//留存分析
				case "menu_retained":
				config.viewType = "block";
				config.type = "retention";
				break;

				//crash
				case "menu_collapse":
				config.type = "crash";
			            config.viewType = "line";
			            break;

				//时长分析
				case "menu_timedist":
				moreline = false;
				config.param.product = "itoolsv1"
				config.type = "timedist";
				config.viewType = "line";
				break;

				//下载速度分析
				case "menu_down_speed":
				menuHour.show();
				config.param.product = "itoolsv1"
				config.type = "dlapp";
				config.viewType = "line";
				config.param.start = defaultDate.start;
				config.param.end = defaultDate.start;
				config.param.granularity = "hour";
				break;

				//下载量分析
				case "menu_down_count":
				menuHour.show();
				config.param.product = "itoolsv1"
				config.type = "dlapp";
				config.viewType = "morebar";
				config.param.start = defaultDate.start;
				config.param.end = defaultDate.start;
				config.param.granularity = "hour";
				break;
                                
                                //ipa2exe分析
				case "menu_ipa2exe":
				//menuHour.show();
				config.param.product = "ipa2exe"
                                config.type = "ipa2exe";
                                config.viewType = "line";
                                config.url = "http://stat2.itools.hk/report2";
				break;
                                
                                //其他统计分析（功能点统计）
				case "menu_function":
                                //menuHour.show();
				config.param.product = "function"
                                config.type = "function";
                                config.viewType = "line";
                                config.url = "http://127.0.0.1/statistics_script/report.php";
                                config.url = "http://stat2.itools.hk/report2";
				break;
			}
			
			init();
		});

        
		//add_btn
		add_btn.click(function() {
			_chartLock = true;
			option_list.add();
		});

		$("#time_selection button").click(function(){
			var _this = $(this);
			if (_chartLock) {
				option_list.clear();
				option_list.add();
			}
			// $("#time_selection button").removeClass("active");
			// _this.addClass("active");
			switch(_this.html()){
				case "小时":
				config.param.granularity = "hour";
				break;

				case "日":
				config.param.granularity = "day";
				break;

				case "周":
				config.param.granularity = "week";
				break;

				case "月":
				config.param.granularity = "month";
				break;

				case "7日":
				config.param.granularity = "day";
			            config.param.start = time_7.end;
			            config.param.end = time_7.start;
				break;

				case "30日":
				config.param.granularity = "day";
			            config.param.start = time_30.end;
			            config.param.end = time_30.start;
				break;
			}
			if (moreline) {
				_chartLock = false;
				option_list.add();
			}
			init();
		});
		

		

		//下拉选项 语言
		menuLanguage.click(function(e) {
			if (e.target.nodeName == "A") {
				menuVersion.find(".select_text").html("全部");
				menuVersion.find("li").remove();
				delete config.param.v;
				if ($(e.target).attr("value") == "del") {
					delete config.param.l;
				} else {
					config.param.l = Number($(e.target).attr("value"));
				}
				optionsEvent(e);
			};

		});

		//下拉选项 版本
		menuVersion.click(function(e) {
			if (e.target.nodeName == "A") {
				if ($(e.target).attr("value") == "del") {
					delete config.param.v;
				} else {
					config.param.v = Number($(e.target).attr("value"));
				}
				optionsEvent(e);
			};
		});

		//下拉选项 产品
		menuChannel.click(function(e) {
			if (e.target.nodeName == "A") {
				menuVersion.find(".select_text").html("全部");
				menuVersion.find("li").remove();
				menuLanguage.find(".select_text").html("全部");
				menuLanguage.find("li").remove();
				// debug.log($(e.target).attr("value"));
				delete config.param.l;
				delete config.param.v;
				if ($(e.target).attr("value") == "del") {
					delete config.param.p;
				} else {            
                                        if( Number($(e.target).attr("value"))){
                                            config.param.p = Number($(e.target).attr("value"));
                                        }else{
                                            config.param.p = $(e.target).attr("value");
                                        }					
				}
				optionsEvent(e);
			};
		});

		//生成下拉选项
		var createMenu = function() {
			debug.log(_options);
			for (var i in _options) {
				
				var all = "<li><a href='javascript:void(0)' value='del'>全部</a></li>"
				switch (i) {
					case "p":
						var innerHTML = "";
                                                if(_options[i][0]){
                                                    for (var j = 0, max = _options[i].length; j < max; j++) {
                                                        if(productTit[_options[i][j]]){
                                                            innerHTML += "<li><a href='javascript:void(0)' value='" + _options[i][j] + "' >" + productTit[_options[i][j]] + "</a></li>";
                                                        }else{
                                                            innerHTML += "<li><a href='javascript:void(0)' value='" + _options[i][j] + "' >" + _options[i][j] + "</a></li>";
                                                        }
                                                    }
                                                }else{
                                                    for (key in _options[i]){
                                                        innerHTML += "<li><a href='javascript:void(0)' value='" + key + "' >" +_options[i][key] + "</a></li>";
                                                    }
                                                }
						menuChannel.find(".dropdown-menu").html(all + innerHTML);
						break;
					case "l":
						var innerHTML = "";
						var _tit = (config.param.p == "2" || config.param.p == "7") ? languageTit["mac"] : languageTit["win"];
						for (var j = 0, max = _options[i].length; j < max; j++) {
							innerHTML += "<li><a href='javascript:void(0)' value='" + _options[i][j] + "' >" + _tit[_options[i][j]] + "</a></li>";
						}
						menuLanguage.find(".dropdown-menu").html(all + innerHTML);
						break;
					case "v":
						var innerHTML = "";
						for (var j = 0, max = _options[i].length; j < max; j++) {
							innerHTML += "<li><a href='javascript:void(0)' value='" + _options[i][j] + "' >" + _options[i][j] + "</a></li>";
						}
						menuVersion.find(".dropdown-menu").html(all + innerHTML);
						break;
				}
			}


			dropdownMenu.each(function() {
				if ($(this).find("li").size() <= 0 || config.type == "retention") {
					$(this).parent().find("button").attr("disabled", "true");
				} else {
					$(this).parent().find("button").removeAttr("disabled");
				}
			});
		}

		$("#s_time").datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			"maxDate": (new Date()),
			onClose: function(selectedDate) {
				$("#e_time").datepicker("option", "minDate", selectedDate);
			},
			onSelect: function(selectedDate) {
				config.param.start = selectedDate;
				if (moreline) {
					_chartLock = false;
					option_list.add();
				}
				init();
			}
		});
		$("#e_time").datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			"maxDate": (new Date()),
			onClose: function(selectedDate) {
				$("#s_time").datepicker("option", "maxDate", selectedDate);
			},
			onSelect: function(selectedDate) {
				config.param.end = selectedDate;
				if (moreline) {
					_chartLock = false;
					option_list.add();
				}
				init();
			}
		});
		
		return {
			delChart: function(e) {
				var trs = option_list.find("tbody").find("tr");
				var select = $(e).parent().parent();

				if (trs.length <= 1) {
					v.clearChart();
					return false;
				}
				trs.each(function(f) {
					if (select.html() == $(this).html()) {
						$(this).remove();
						var _x = 0;
						for (var i in _data.xdata) {
							if (_x == f) {
								delete _data.xdata[i];
							}
							_x++;
						}
						init();
					}
				});
			}
		}
	};



//活跃用户概况curl http://stat2.itools.hk/report -d '{"op":"stat", "type":"activation", "param":{"product":"itoolsv1"}}'
//callback2({"yestoday": 377484, "last30days": 7189479, "last7days": 2710368})
var activation_module = (function() {
	var base64 = new Base64();
	var _data = {
		"op": "stat",
		"type": "activation",
		"param": {
			"product": "itoolsv1"
		}
	};
	var _o = $("#active_message").find("tbody").find("td");


	function init() {
		var base64_data = base64.encode(JSON.stringify(_data));
		$.ajax({
			url: "http://stat2.itools.hk/report",
			dataType: "jsonp",
			jsonp: "callback",
			data: {
				"req": base64_data
			},
			success: function(msg) {}
		});
	}


	window.callback2 = function(msg) {
		_o.eq(0).html(msg.yestoday);
		_o.eq(1).html(msg.last7days);
		_o.eq(2).html((msg.yestoday / msg.last7days * 100).toFixed(2) + "%");
		_o.eq(3).html(msg.last30days);
		_o.eq(4).html((msg.yestoday / msg.last30days * 100).toFixed(2) + "%");
	}


	init();

	return {
		reset: function(p) {
			_data.param.product = p;
			init();
		}
	}
}());